package com.example.demo.controller;

import com.example.demo.service.User.UserService;
import com.example.demo.service.User.UserServiceImpl;
import com.ruiyun.jvppeteer.core.Puppeteer;
import com.ruiyun.jvppeteer.core.browser.Browser;
import com.ruiyun.jvppeteer.core.browser.BrowserFetcher;
import com.ruiyun.jvppeteer.core.page.Page;
import com.ruiyun.jvppeteer.options.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.ExecutionException;

@Controller
@RequestMapping("/hello")
public class HelloController {
    @Autowired
    @Qualifier("teacherService")
    private UserService userService;//当项目需要调用一个接口，不同的方法时可以这样使用
    /*private ShiroUserServiceImpl ShiroUserServiceImpl;*///也可以这样直接调用service

    @RequestMapping("/Hello")
    public String Hello() {
        return "登录模板";

    }

    @RequestMapping("/name")
    @ResponseBody
    public String name() {
        return userService.name();
        /*return ShiroUserServiceImpl.name();*/
    }

    @RequestMapping("test")
    @ResponseBody
    public String test() throws IOException, ExecutionException, InterruptedException {
        long l = System.currentTimeMillis();
        //自动下载，第一次下载后不会再下载
        BrowserFetcher.downloadIfNotExist(null);
        ArrayList<String> arrayList = new ArrayList<>();
        //生成pdf必须在无厘头模式下才能生效
        LaunchOptions options = new LaunchOptionsBuilder().withHeadless(true).withArgs(arrayList).withViewport(new Viewport(1024, 600, 1, false, false, false)).build();
        arrayList.add("--no-sandbox");
        arrayList.add("--disable-setuid-sandbox");
        Browser browser = Puppeteer.launch(options);
        Page page = browser.newPage();
        page.goTo("https://www.qq.com/");
        ScreenshotOptions screenshotOptions = new ScreenshotOptions();
        /*screenshotOptions.setFullPage(Boolean.TRUE);*///全屏截图
        //设置截图范围
        Clip clip = new Clip(0, 0, 1024, 600);

        screenshotOptions.setClip(clip);
        //设置存放的路径
        screenshotOptions.setType("png");
        screenshotOptions.setPath("/root/test.png");
        page.screenshot(screenshotOptions);
        System.out.println(System.currentTimeMillis() - l + "毫秒");
        page.close();
        browser.close();
        return "success";
    }

    @RequestMapping("file")
    @ResponseBody
    public String filetest() {
        File file = new File("User.java");
        return file.getAbsolutePath();
    }
}
